<?php

namespace App\Common\OA;


use App\Common\Log;
use App\Model\t_s_account;
use App\Model\t_s_app;
use EasySwoole\Component\Openssl;
use XWX\Common\H;

class OAUser extends _Base
{
    public $acc_id;
    public $x;
    public $nonce_str;
    public $redis_key;


    public function __construct($token)
    {
        $this->redis_key = $token;

        $string1 = $this->redis()->get($this->redis_key);
        if (H::funcStrHasAnyText($string1))
        {
            $data = (array)json_decode($string1);

            $vars = get_class_vars(OAUser::class);
            foreach ($vars as $key => $val)
            {
                $__key = "OA__{$key}";
                $val = H::funcArrayGet($data, $__key);
                if (H::funcStrHasAnyText($val))
                {
                    $this->$key = $val;
                }
            }

            $this->funcSetX($this->x);
        }
    }


    /**
     * 管理员
     *
     * @return t_s_account
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function funcGet__acc()
    {
        $entity = $this->context()->get($this->acc_id);
        if ($entity === null)
        {
            $entity = t_s_account::funcXLoad($this->db(), $this->x, $this->acc_id);
        }

        return $entity;
    }


    /**
     * 身份是否超管
     *
     * @return bool
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function isSuperAdmin()
    {
        return $this->funcGet__acc()->acc_role == 'super_admin';
    }


    /**
     * 检查签名
     *
     * @param $token
     * @return bool
     */
    public function funcCheckSign($token)
    {
        $sign = 'OA:' . OA::funcAdminGetSign($this->x, $this->acc_id, $this->nonce_str);
        if ($sign === $token)
        {
            return true;
        }

        Log::funcLogErr(__METHOD__, "sign error");
        Log::funcLogDebug(__METHOD__, "token = {$token}");
        Log::funcLogDebug(__METHOD__, "sign = {$sign}");

        return false;
    }


    /**
     * 应用列表
     *
     * @return \EasySwoole\Mysqli\Mysqli|mixed
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\OrderByFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function funcGetApps()
    {
        $list = $this->funcGetAppQuery()
            ->orderBy('app_order', 'ASC')
            ->get(t_s_app::table());

        return $list;
    }

    /**
     * 是否拥有应用权限
     *
     * @param $app_id
     * @return bool
     * @throws \Throwable
     */
    public function funcCheckRoleByAppID($app_id)
    {
        $count = $this->funcGetAppQuery()
            ->where('app_id', $app_id)
            ->count(t_s_app::table());
        if ($count > 0)
        {
            return true;
        }


        return false;
    }


    /**
     * 应用查询
     * @return \EasySwoole\Mysqli\Mysqli
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    protected function funcGetAppQuery()
    {
        $types = ['common', $this->funcGetX__config()->aconfig_type];

        $query = $this->db()
            ->whereIn('app_type', $types)
            ->where('app_enable', 1);

        return $query;
    }
}